package com.xiaoming.freshmenapp.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.xiaoming.freshmenapp.R;
import com.xiaoming.freshmenapp.bean.MessagesBean;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;

public class AddMessagesActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView home_back;
    private TextView home_title;
    private LinearLayout home_layout;
    private EditText addmsg_et_title;
    private EditText addmsg_et_content;
    private Button addmsg_et_btn;
    private String title;
    private String content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_messages);
        initView();
    }

    private void initView() {
        home_back = (ImageView) findViewById(R.id.home_back);
        home_title = (TextView) findViewById(R.id.home_title);
        home_title.setText("我要留言");
        home_layout = (LinearLayout) findViewById(R.id.home_layout);
        home_layout.setBackground(getResources().getDrawable(R.color.lvse));
        addmsg_et_title = (EditText) findViewById(R.id.addmsg_et_title);
        addmsg_et_content = (EditText) findViewById(R.id.addmsg_et_content);
        addmsg_et_btn = (Button) findViewById(R.id.addmsg_et_btn);

        addmsg_et_btn.setOnClickListener(this);
        home_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.addmsg_et_btn:
                submit();
                getData();
                break;
            case R.id.home_back:
                finish();
                break;
        }
    }

    private void getData() {

        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());

        String url = "http://10.2.64.83:8080/app8/messages/add";

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("title",title);
            jsonObject.put("content",content);
            jsonObject.put("postDate",new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
            jsonObject.put("name","李大爷");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.e("TAG", "getData: "+jsonObject.toString() );
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e("TAG", "onResponse: "+jsonObject );
                Gson gson = new Gson();
                MessagesBean messagesBean = gson.fromJson(jsonObject.toString(), MessagesBean.class);
                if (messagesBean.isSuccess()){
                    Toast.makeText(AddMessagesActivity.this, "成功", Toast.LENGTH_SHORT).show();
                    finish();
                }else {
                    Toast.makeText(AddMessagesActivity.this, "失败", Toast.LENGTH_SHORT).show();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(AddMessagesActivity.this, "网络错误"+volleyError.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

        queue.add(request);

    }

    private boolean submit() {
        // validate
        title = addmsg_et_title.getText().toString().trim();
        if (TextUtils.isEmpty(title)) {
            Toast.makeText(this, "请填入标题", Toast.LENGTH_SHORT).show();
            return false;
        }

        content = addmsg_et_content.getText().toString().trim();
        if (TextUtils.isEmpty(content)) {
            Toast.makeText(this, "请填入内容", Toast.LENGTH_SHORT).show();
            return false;
        }

       return true;


    }
}